home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17119 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  64 lines

  1. Path: ix.netcom.com!news
  2. From: giuliano@ix.netcom.com(Giuliano Carlini)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Default Constructors
  5. Date: 13 Apr 1996 08:54:32 GMT
  6. Organization: Netcom
  7. Message-ID: <4knq48$fn6@dfw-ixnews1.ix.netcom.com>
  8. References: <316F2B88.5FC4@psych.stanford.edu>
  9. NNTP-Posting-Host: lbx-ca8-13.ix.netcom.com
  10. X-NETCOM-Date: Sat Apr 13  3:54:32 AM CDT 1996
  11.  
  12. In <316F2B88.5FC4@psych.stanford.edu> Nick Cassimatis
  13. <nick@psych.stanford.edu> writes: 
  14. >
  15. >I'm having trouble getting default constructors to work in another 
  16. >class' constructor: e.g.,
  17. >
  18. >class position {
  19. >    int x,y,z;
  20. >    position() {x = y = z = 0;};
  21. >    position(int, int, int);
  22. >};
  23. >
  24. >class object {
  25. >    position pos;
  26. >    object();
  27. >};
  28. >
  29. >object::object() {
  30. >    pos.x = 1;
  31. >}
  32. >
  33. >I get the following error messages (in BC4.5):
  34. >
  35. >'position::position()' is not accessible in function object::object()
  36. >'position::x' is not accessible in function object::object()
  37. >
  38. >What exactly is meant by "access"? And why wouldn't I have it to an 
  39. >object that's already been declared?
  40. >
  41. >-Nick
  42.  
  43. Your problem is that the members of position - and object for that
  44. matter - are all private. That is, only a member's of "this" may be
  45. changed. That is what the error message means. The client code can't
  46. access the designated member. The solution is:
  47. class position {
  48. public:
  49.     int x,y,z;
  50.     position() {x = y = z = 0;};
  51.     position(int, int, int);
  52. }
  53.  
  54. Classes are often designed so that some parts are public, and therefore
  55. callable by clients, and other parts are protected or private, and
  56. therefore illegal from clients to access. Public members are part of
  57. the interface, protected and private part of the implementation.
  58.  
  59. There is also a difference between protected and private. A protected
  60. member may be accessed by a subclass's function, while a private member
  61. may be accessed only by a function of the class, and not by a subclass.
  62.  
  63. g
  64.